<template>
  <div class="details">
    <Crumd :nav="navArr"></Crumd>
    <div class="details_top wrap">
      <div class="l">
        <div class="l_l">
          <div class="da_img">
            <img :src="imgB + productInfo.coverImg" width="459px" alt="" />
          </div>
          <div class="xiao_img">
            <div
              v-for="(item, index) in productInfo.imgAltas"
              :key="item.index"
            >
              <img
                :src="imgB + item.src"
                width="100px"
                alt=""
                :style="{ opacity: num == index ? 1 : 0.5 }"
                @mouseenter="imgTap(index, item.src)"
              />
            </div>
          </div>
        </div>
        <div class="l_r">
          <h2>{{ productInfo.seriesTitle }}</h2>
          <span class="span_1">{{ productInfo.seriesSubTitle }}</span>
          <div class="jinfen">{{ productInfo.coin }}积分</div>
          <div class="foor">
            <section
              v-for="(item, index) in productInfo.parameterJson"
              :key="index"
            >
              <span class="span_2">选择{{ item.title }}</span>
              <ul>
                <li
                  v-for="val in item.parameters"
                  :key="val.id"
                  @click="youCanBuyClick(val.id)"
                  :class="val.currentActivate ? 'active' : ''"
                >
                  {{ val.title }}
                </li>
              </ul>
            </section>
            <span class="span_3">数量 *礼品库存{{ productInfo.stock }}件</span>
            <div class="button">
              <button @click="stepFn(-1)">-</button>
              <div class="num">{{ stepNum }}</div>
              <button @click="stepFn(1)">+</button>
            </div>

            <div class="button2">
              <div class="an1" @click="addToCart">加入购物车</div>
              <div class="an2">立即购买</div>
            </div>
          </div>
        </div>
      </div>
      <div class="r">
        <h3>你还可以兑换</h3>
        <ul>
          <li
            v-for="item in arr"
            :key="item.id"
            @click="youCanBuyClick(item.id)"
          >
            <div class="div_1">
              <img :src="imgB + item.img" alt="" width="80px" height="80px" />
            </div>
            <div class="div_2">
              <h4>{{ item.name }}</h4>
              <span
                ><i>{{ item.coin }}</i
                >积分</span
              >
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="btn wrap">
      <div class="btn1" :class="bool ? 'active' : ''" @click="add1">
        商品详细
      </div>
      <div @click="add2" :class="!bool ? 'active' : ''">常见问题</div>
    </div>
    <div class="box wrap">
      <div class="box1" v-show="bool" v-html="productInfo.description"></div>
      <div class="box2" v-show="!bool">
        <h2>兑换后运费如何收取？</h2>
        商城内大部分实物产品均可包邮，也支持上门自提。虚拟产品则无需运费，可在兑换后直接使用。
        （注：新疆，西藏，内蒙古，青海，海南，宁夏暂不支持包邮，在此地区兑换商品需提前与客服确认运费。）
        如何申请退换货？ 所有产品均为鸡腿兑换礼品，无质量问题不支持退换货；
        如因喜好不同，或到货后后悔等原因，不支持退货；
        因客户原因如联系方式有误、电话停机、不接电话、无理由拒收等造成快件无法正常签收的情况，叩丁狼均不进行补发或赔付；
        如因质量问题需要退货，将由客服人员与客户联系，确认邮寄地址与流程。退回物品不得有人为损坏或使用痕迹，否则拒绝退换；
        如顾客选择自行寄回商品，请先垫付运费，到货验证商品后，叩丁狼将以鸡腿发放形式为用户报销运费，不接受单方面到付件。
        <h2>如何开具发票？</h2>
        叩丁狼积分商城产品均为定制礼品，使用积分兑换的产品均不支持开票。
        <h2>关于鸡腿价格问题</h2>
        因可能存在系统缓存、页面更新延迟等不确定性情况，导致价格显示异常，商品具体售价请以订单结算页价格为准。如您发现异常情况出现，请立即联系我们补正，以便您能顺兑换。
        联系我们 www.wolfcode.cn 微信公众号搜索“叩丁狼”
      </div>
    </div>
  </div>
</template>

<script>
import { GoodsDetailsAPI, AddToCartAPI } from "../request/api.js";
import Crumd from "../components/Crumd.vue";
import { mapActions } from "vuex";
export default {
  components: {
    Crumd,
  },
  data() {
    return {
      bool: true,
      //商品信息
      productInfo: {},
      arr: [],
      num: 0,
      stepNum: 1,
      navArr: [],
    };
  },
  created() {
    document.documentElement.scrollTop = 0;
    let goodsId = this.$route.query.id;
    this.youCanBuyClick(goodsId);
  },
  methods: {
    ...mapActions({
      asyncChenIsShowToast: "showToast/asyncChenIsShowToast",
      asyncChenUserInfo: "userInfo/asyncChenUserInfo",
    }),
    async addToCart() {
      let goodsId = this.$route.query.id;
      if (this.stepNum > 5) {
        this.asyncChenIsShowToast({
          type: "danger",
          msg: "商品不能超过5件",
        });
        return;
      }
      let res = await AddToCartAPI({
        productId: goodsId,
        total: this.stepNum,
        modified: 1,
      });
      if (!res) return;
      this.asyncChenIsShowToast({
        type: "success",
        msg: "成功加入购物车!",
      });
      // this.asyncChenUserInfo()
      this.$emit("fn");
    },
    stepFn(val) {
      /* if(val<0&&this.stepNum>1 ||val>0&&this.stepNum<this.productInfo.stock){
        this.stepNum+=val
      } */
      if (
        (val == -1 && this.stepNum == 1) ||
        (val == 1 && this.stepNum >= this.productInfo.stock)
      ) {
        return;
      }
      this.stepNum += val;
    },
    add1() {
      this.bool = true;
    },
    add2() {
      this.bool = false;
    },
    async youCanBuyClick(id) {
      this.$router.push(`/details?id=${id}`);
      let res = await GoodsDetailsAPI(id);
      if (!res) return;
      this.arr = res.data.themYouCanBuy;
      this.productInfo = res.data.productInfo;
      this.navArr = res.data.nav;

      //初始化
      this.productInfo.coverImg = this.productInfo.imgAltas[0].src;
      this.num = 0;
      //初始化stepNum
      this.stepNum = 1;

      //替换商品详情图片的连接
      this.productInfo.description = this.productInfo.description.replaceAll(
        "upload/",
        this.imgB + "/upload/"
      );
    },
    imgTap(i, src) {
      this.num = i;
      this.productInfo.coverImg = src;
    },
  },
};
</script>
 
<style lang = "less" scoped>
.details {
  background-color: rgb(245, 245, 245);
  .details_top {
    display: flex;
    height: 660px;
    padding-bottom: 52px;
    .l {
      flex: 1;
      padding-right: 49px;
      display: flex;
      border-right: 1px solid #ccc;
      .l_l {
        flex: 1;
        height: 100%;
        margin-right: 32px;
        .xiao_img {
          padding-top: 20px;
          display: flex;
          justify-content: space-between;
          cursor: pointer;
        }
      }
      .l_r {
        width: 430px;
        height: 100%;
        h2 {
          font-size: 28px;
          font-family: SourceHanSansSC;
          font-weight: 400;
          color: #000000;
          line-height: 23px;
        }
        .span_1 {
          height: 35px;
          font-size: 14px;
          font-family: SourceHanSansSC;
          font-weight: 300;
          color: #999999;
          line-height: 20px;
          margin: 15px 0 26px;
          display: inline-block;
        }
        .jinfen {
          font-size: 28px;
          font-family: SourceHanSansSC;
          font-weight: bold;
          color: #fd604d;
          padding-bottom: 23px;
        }
        .foor {
          border-top: 1px solid #ccc;
          span {
            margin: 19px 0 14px;
            display: inline-block;
          }
          ul {
            display: flex;
            flex-wrap: wrap;
            li {
              height: 32px;
              display: inline-block;
              padding: 0 10px;
              border: 1px solid #0a328e;
              margin-right: 10px;
              margin-bottom: 10px;
              line-height: 32px;
              text-align: center;
              cursor: pointer;
            }
          }

          .button {
            display: flex;
            cursor: pointer;
            button {
              width: 30px;
              height: 30px;
              text-align: center;
              line-height: 30px;
              border: 1px solid #d1d1d1;
              box-sizing: border-box;
              color: #999;
              cursor: pointer;
            }
            .num {
              width: 45px;
              height: 30px;
              text-align: center;
              line-height: 30px;
              border: 1px solid #d1d1d1;
              box-sizing: border-box;
              color: #999;
            }
          }

          .button2 {
            display: flex;
            margin-top: 27px;
            color: #fff;
            .an1 {
              width: 150px;
              height: 46px;
              background: #3dc36b;
              margin-right: 19px;
              line-height: 46px;
              text-align: center;
              cursor: pointer;
            }
            .an2 {
              width: 150px;
              height: 46px;
              line-height: 46px;
              text-align: center;
              background: #fd604d;
              cursor: pointer;
            }
          }
        }
      }
    }
    .r {
      width: 230px;
    }
  }

  .r {
    padding-left: 20px;
    h3 {
      margin-bottom: 19px;
      font-size: 18px;
      font-family: SourceHanSansSC;
      font-weight: 400;
      color: #333333;
      line-height: 20px;
    }
    ul {
      li {
        display: flex;
        height: 100px;
        margin-bottom: 10px;
        .div_1 {
          width: 81px;
          height: 81px;
        }
        .div_2 {
          margin-left: 10px;
          h4 {
            font-size: 14px;
            font-family: SourceHanSansSC;
            font-weight: 900;
            color: #ccc;
            line-height: 20px;
            text-overflow: ellipsis;
            white-space: nowrap;
            overflow: hidden;
          }
          span {
            display: inline-block;
            margin: 14px 0;
            font-size: 18px;
            font-family: SourceHanSansSC;
            font-weight: bold;
            line-height: 20px;
            i {
              color: #ff5e0f;
            }
          }
        }
      }
      li:hover .div_1 img {
        border: 1px solid #000;
      }
      li:hover .div_2 h4 {
        color: #0a328e;
      }
    }
  }
}
.btn {
  border-top: 1px solid #ccc;
  display: flex;
  div {
    width: 120px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    cursor: pointer;
  }
  .btn1 {
    font-weight: 800;
  }
}
.box {
  line-height: 30px;
  padding-bottom: 20px;
  h2 {
    font-weight: 700;
    margin-top: 10px;
    margin-bottom: 10px;
    line-height: 30px;
  }
}
.active {
  background-color: #0a328e;
  color: #fff;
}
</style>